package com.itheima.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.service.BlogService;
import com.itheima.pojo.*;
import com.itheima.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/blog")
public class BlogController {

     @Autowired
     private BlogService blogService;

    //1. 增加博客 /blog/add
    @RequestMapping("/add")
    public Result add(@RequestBody BlogVO blogVO, HttpServletRequest request){
        Map<String, Object> userInfo;
        try {
            userInfo = TokenUtil.getUserInfo(request);
            String uid = (String)userInfo.get("uid");
            blogVO.setAdminUid(uid);
            blogService.add(blogVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "插入失败");
        }
        return new Result(StatusCode.OK, "插入成功");
    }


    //2. 删除博客   POST /blog/delete
    @RequestMapping("/delete")
    public Result delete(@RequestBody BlogVO blogVO){
        try {
            blogService.delete(blogVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "删除失败");
        }
        return new Result(StatusCode.OK, "删除成功");
    }


    //3. 删除选中博客   POST /blog/deleteBatch
    @RequestMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<BlogVO> list){
        try {
            blogService.deleteBatch(list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "删除失败");
        }
        return new Result(StatusCode.OK, "删除成功");
    }


    //4. 编辑博客      POST /blog/edit
    @RequestMapping("/edit")
    public Result edit(@RequestBody BlogVO blogVO){
        try {
            blogService.edit(blogVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR, "更新失败");
        }
        return new Result(StatusCode.OK, "更新成功");
    }


    //5. 获取博客列表    /blog/getList
    @RequestMapping("/getList")
    public Map getList(@RequestBody BlogVO blogVO) throws Exception {

        PageInfo<Blog> page=  blogService.getList(blogVO);

        List<Blog> blogList = page.getList();
        long total = page.getTotal();

        Map<String, Object> map = new HashMap<>();

        //封装查询到的数据
        List<Map<String,Object>> list=new ArrayList<>();

        for (Blog blog : blogList) {

            Map<String, Object> map1 = new HashMap<>();

            map1.put("title",blog.getTitle());
            map1.put("summary",blog.getSummary());
            map1.put("content",blog.getContent());
            map1.put("tagUid",blog.getTagUid());
            map1.put("blogSortUid",blog.getBlogSortUid());
            map1.put("clickCount",blog.getClickCount());
            map1.put("collectCount",blog.getCollectCount());
            map1.put("author",blog.getAuthor());
            map1.put("isPublish",blog.getIsPublish());
            map1.put("isOriginal",blog.getIsOriginal());
            map1.put("level",blog.getLevel());
            map1.put("sort",blog.getSort());
            map1.put("startComment",blog.getStartComment());

            List<Tag> tagList2 = blog.getTagList();
            List<Map<String,Object>> list3=new ArrayList<>();
            for (Tag tag : tagList2) {
                Map<String, Object> map3 = new HashMap<>();
               // {"content":"测试标签","clickCount":18,"sort":18,"uid":"5c4c541e600ff422ccb371ee788f59d6","status":1,"createTime":"2020-08-05 16:51:11","updateTime":"2020-08-05 16:51:11"}]
                map3.put("content",tag.getContent());
                map3.put("clickCount",tag.getClickCount());
                map3.put("sort",tag.getSort());
                map3.put("uid",tag.getUid());
                map3.put("status",tag.getStatus());
                map3.put("createTime", DateUtils.parseDate2String(tag.getCreateTime()));
                map3.put("updateTime",DateUtils.parseDate2String(tag.getUpdateTime()));
                list3.add(map3);
            }

            map1.put("tagList",list3);

            List<String> photoList = blog.getPhotoList();
            map1.put(" photoList", photoList);

            BlogSort blogSort = blog.getBlogSort();

            Map<String, Object> map4 = new HashMap<>();
            //{"sortName":"测试3","content":"测试3","clickCount":2,"sort":10,"uid":"718c76d4a84e408da23e04949a75e7e8","status":1,"createTime":"2020-03-31 20:08:00","updateTime":"2020-08-05 16:39:56"}
            map4.put("sortName",blogSort.getSortName());
            map4.put("content",blogSort.getContent());
            map4.put("clickCount",blogSort.getClickCount());
            map4.put("sort",blogSort.getSort());
            map4.put("uid",blogSort.getUid());
            map4.put("status",blogSort.getStatus());
            map4.put("createTime",DateUtils.parseDate2String(blogSort.getCreateTime()));
            map4.put("updateTime",DateUtils.parseDate2String(blogSort.getUpdateTime()));
            map1.put("blogSort",map4);

            //"uid":"07d5a3bfdbb1267904c6bcf2837761cd","status":1,"createTime":"2020-04-24 08:47:54","updateTime":"2020-08-05 16:32:21"
            map1.put("uid",blog.getUid());
            map1.put("status",blog.getStatus());
            map1.put("createTime",DateUtils.parseDate2String(blog.getCreateTime()));
            map1.put("updateTime",DateUtils.parseDate2String(blog.getUpdateTime()));
            list.add(map1);
        }

        map.put("records", list );

        map.put("total", total);
        map.put("size", blogVO.getPageSize());
        map.put("current", blogVO.getCurrentPage());

        ArrayList<Object> list2 = new ArrayList<>();
        //"code":"success","data":{"records":[{"title":"hushisheng","summary":"didididi","content":"嗨起来","tagUid":"5c4c541e600ff422ccb371ee788f59d6","blogSortUid":"718c76d4a84e408da23e04949a75e7e8","clickCount":1,"collectCount":0,"isPublish":"1","isOriginal":"1","level":0,"sort":0,"startComment":"1","tagList":[{"content":"测试标签","clickCount":18,"sort":18,"uid":"5c4c541e600ff422ccb371ee788f59d6","status":1,"createTime":"2020-08-05 16:51:11","updateTime":"2020-08-05 16:51:11"}],"blogSort":{"sortName":"测试3","content":"测试3","clickCount":2,"sort":10,"uid":"718c76d4a84e408da23e04949a75e7e8","status":1,"createTime":"2020-03-31 20:08:00","updateTime":"2020-08-05 16:39:56"},"uid":"1281207210759819264","status":1,"createTime":"2020-07-09 20:42:57","updateTime":"2020-07-15 14:51:42"}
        map.put("orders", list2);
        map.put("optimizeCountSql", true);
        map.put("isSearchCount", true);

        Map<String, Object> result = new HashMap<>();

        result.put("code", "success");
        result.put("data", map);
        return result;
    }

}
